package com.spicdt.edge.protocol.door.demo.callback.dto;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述: 用户认证信息
 *
 * @author ZlTan
 * @date 2025/04/07 19:56
 */
@Data

public class PersonAuthorizeDto implements Serializable {
    private static final long serialVersionUID = -1815363768326415191L;

    //授权信息
    private List<Authority> authorityList;
    //证件号
    private String authenticateCode;
    //证件类型:111(身份证),414(护照),113(户口簿),335(驾驶证),131(工作证),133(学生证),990(其他)，不传默认身份证
    private String authenticateType;
    //人脸照片
    private String photo;
    //用户id
    private String personId;
    //手机号
    private String phone;
    //1.新增;2.修改;3.删除
    private Integer opType;

    public List<AuthorizeInfo> transfer() {
        return this.authorityList.stream().map(auth ->
                        auth.getDeviceList().stream().map(device -> AuthorizeInfo.builder()
                                        .mjdId(device.getDeviceCode())
                                        .mjTyp(AuthorizeInfo.MjTyp.PERSON.getCode())
                                        .mjdName(device.getDeviceName())
                                        .startTime(auth.getStartTime())
                                        .endTime(auth.getEndTime())
                                        .userIdCard(authenticateCode)
                                        .usrCod(personId)
                                        .phone(phone)
                                        .build())
                                .collect(Collectors.toList())
                ).flatMap(Collection::stream)
                .collect(Collectors.toList());
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("PersonAuthorizeDto{\n");
        sb.append("  personId='").append(personId).append("',\n");
        sb.append("  phone='").append(phone).append("',\n");
        sb.append("  authenticateCode='").append(authenticateCode).append("',\n");
        sb.append("  authenticateType='").append(authenticateType).append("',\n");
        sb.append("  opType=").append(opType).append(",\n");
        sb.append("  photo='").append(photo != null ? "[照片数据]" : "null").append("',\n");

        sb.append("  authorityList=[\n");
        if (authorityList != null) {
            for (int i = 0; i < authorityList.size(); i++) {
                Authority auth = authorityList.get(i);
                sb.append("    {\n");
                sb.append("      accLevelName='").append(auth.getAccLevelName()).append("',\n");
                sb.append("      startTime='").append(auth.getStartTime()).append("',\n");
                sb.append("      endTime='").append(auth.getEndTime()).append("',\n");
                sb.append("      deviceList=[\n");

                if (auth.getDeviceList() != null) {
                    for (int j = 0; j < auth.getDeviceList().size(); j++) {
                        DeviceInfo device = auth.getDeviceList().get(j);
                        sb.append("        {\n");
                        sb.append("          deviceCode='").append(device.getDeviceCode()).append("',\n");
                        sb.append("          deviceName='").append(device.getDeviceName()).append("'\n");
                        sb.append("        }");
                        if (j < auth.getDeviceList().size() - 1) {
                            sb.append(",");
                        }
                        sb.append("\n");
                    }
                }

                sb.append("      ]\n");
                sb.append("    }");
                if (i < authorityList.size() - 1) {
                    sb.append(",");
                }
                sb.append("\n");
            }
        }
        sb.append("  ]\n");
        sb.append("}");
        return sb.toString();
    }

    @Data
    public static class Authority implements Serializable {
        private static final long serialVersionUID = -9135309504650823546L;
        //权限组
        private String accLevelName;
        //设备列表
        private List<DeviceInfo> deviceList;

        //授权开始时间,多个以逗号分隔
        private String startTime;
        //授权结束时间,多个以逗号分隔
        private String endTime;
    }

    @Data
    public static class DeviceInfo implements Serializable {
        private static final long serialVersionUID = 3108887256987582922L;
        //设备编码
        private String deviceCode;
        //设备名称
        private String deviceName;
    }
}
